﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace RPGServer
{
    public class CacheMapService
    {
        public List<RPGSwitchPoint> SwitchPoints;

        public CacheMapService()
        {
            SwitchPoints = new List<RPGSwitchPoint>();
        }

        public void LoadDataFromFile()
        {
            SwitchPoints.Clear();

            string switchPointFile = string.Format("{0}data\\switchpoint.xml", AppDomain.CurrentDomain.BaseDirectory);
            if (!File.Exists(switchPointFile))
            {
                return;
            }

            XDocument xDoc = XDocument.Load(switchPointFile);

            List<XElement> elements = xDoc.Element("switch-points").Elements("item").ToList();
            for (int index = 0; index < elements.Count; index++)
            {
                XElement xItem = elements[index];
                RPGSwitchPoint sp = new RPGSwitchPoint();
                sp.FromMapId = Convert.ToUInt16(xItem.Attribute("from-map").Value);
                // lt(R,C) rb(R,C)
                string[] fromRangeArr = xItem.Attribute("from-range").Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                sp.FromRangeTop = Convert.ToUInt16(fromRangeArr[0].Trim());
                sp.FromRangeLeft = Convert.ToUInt16(fromRangeArr[1].Trim());
                sp.FromRangeBottom = Convert.ToUInt16(fromRangeArr[2].Trim());
                sp.FromRangeRight = Convert.ToUInt16(fromRangeArr[3].Trim());

                sp.ToMapId = Convert.ToUInt16(xItem.Attribute("to-map").Value);
                string[] toPosArr = xItem.Attribute("to-pos").Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                sp.ToPosRow = Convert.ToUInt16(toPosArr[0].Trim());
                sp.ToPosCol = Convert.ToUInt16(toPosArr[1].Trim());

                SwitchPoints.Add(sp);
            }
        }

        /// <summary>
        /// 获取切换点
        /// </summary>
        /// <param name="fromMapId"></param>
        /// <param name="fromPosRow"></param>
        /// <param name="fromPosCol"></param>
        /// <param name="toMapId"></param>
        /// <returns></returns>
        public RPGSwitchPoint GetSwitchPoint(ushort fromMapId, ushort fromPosRow, ushort fromPosCol, ushort toMapId)
        {
            for (int index = 0; index < SwitchPoints.Count; index++)
            {
                RPGSwitchPoint sp = SwitchPoints[index];
                if (sp.FromMapId == fromMapId
                    && sp.ToMapId == toMapId
                    && fromPosRow >= sp.FromRangeTop
                    && fromPosCol >= sp.FromRangeLeft
                    && fromPosRow <= sp.FromRangeBottom
                    && fromPosCol <= sp.FromRangeRight)
                {
                    return sp;
                }
            }
            return null;
        }
    }
}
